package com.lintf.bot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.bot.domain.BotBillCellUser;
import com.lintf.bot.domain.BotItem;
import com.lintf.bot.domain.constant.Constant;
import com.lintf.bot.domain.dto.BotBillCellUpdateDTO;
import com.lintf.bot.domain.dto.BotBillCellUserUpdateDTO;
import com.lintf.bot.domain.enums.NoticeTypeEnums;
import com.lintf.bot.domain.enums.StatusEnums;
import com.lintf.bot.mapper.BotBillCellUserMapper;
import com.lintf.bot.notice.NoticeUtils;
import com.lintf.bot.service.IBotBillCellService;
import com.lintf.bot.service.IBotBillCellUserService;
import com.lintf.bot.service.IBotItemService;
import com.lintf.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lintf
 * @description 针对表【bot_bill_cell_user】的数据库操作Service实现
 * @createDate 2024-02-18 15:37:51
 */
@Service
public class BotBillCellUserServiceImpl extends ServiceImpl<BotBillCellUserMapper, BotBillCellUser>
        implements IBotBillCellUserService {

    @Autowired
    private IBotBillCellService botBillCellService;
    @Autowired
    private IBotItemService botItemService;
    @Autowired
    private NoticeUtils noticeUtils;

    @Override
    @Transactional
    public void update(BotBillCellUserUpdateDTO dto) {
        // 创建BotBillCellUser对象并拷贝属性
        BotBillCellUser billCellUser = new BotBillCellUser();
        BeanUtils.copyProperties(dto, billCellUser);
//        billCellUser.setUpdateParams(); // 设置更新参数
        // 创建BotItem对象并拷贝属性
        BotItem botItem = new BotItem();
        BeanUtils.copyProperties(billCellUser, botItem);
        botItem.setId(null).setRefId(dto.getId()); // 设置ID和引用ID
        // 更新BotBillCellUser和保存BotItem
        baseMapper.updateById(billCellUser);
        botItemService.save(botItem);
        // 设置BotItem的引用ID为BotBillCellUser的ID
        botItem.setRefId(billCellUser.getId());
        // 查询BotBillCellUser并根据状态更新BotBillCell
        LambdaQueryWrapper<BotBillCellUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BotBillCellUser::getBotBillCellId, dto.getBotBillCellId());
        BotBillCellUpdateDTO updateDTO = new BotBillCellUpdateDTO();
        updateDTO.setId(dto.getBotBillCellId());
        if (baseMapper.selectList(wrapper).stream().noneMatch(botBillCell -> botBillCell.getStatus() == StatusEnums.INCOMPLETE.getValue())) {
            updateDTO.setStatus(StatusEnums.COMPLETED.getValue());
        } else {
            updateDTO.setStatus(StatusEnums.INCOMPLETE.getValue());
        }
        botBillCellService.update(updateDTO);
        // 获取当前用户昵称和通知标题
        String currentNickName = SecurityUtils.getLoginUser().getUser().getNickName();
        String noticeTitle = currentNickName + Constant.TO_DO_ITEM_UPDATE;
        // 发送用户通知
        noticeUtils.personPush(dto.getBotBillCellId(), noticeTitle, NoticeTypeEnums.TO_DO_PROCESSING, noticeTitle, Long.parseLong(dto.getCreateBy()), dto.getGroupId());
    }
}




